﻿Public Class Validacao
    Public Function validaremail(ByVal email As String) As Boolean
        Return System.Text.RegularExpressions.Regex.IsMatch(email, ("(?<user>[^@]+)@(?<host>.+)"))
    End Function

    Public Function validarIncricaoESTADUAL(ByVal UF As String, ByVal inscr As String) As Boolean
        Dim retorno As Boolean = False
        Dim strbase As String = ""
        Dim strbase2 As String = ""
        Dim strorigem As String = ""
        Dim strDigito1 As String
        ' Dim strDigito2 As String
        Dim intPos As Integer
        Dim intvalor As Integer
        Dim intsoma As Integer = 0
        Dim intresto As Integer
        'Dim intnumero As Integer
        ' Dim intpeso As Integer
        ' Dim intdig As Integer

        If inscr.ToString.Trim.ToUpper = "ISENTO" Then
            Return True

        End If


        For intPos = 1 To inscr.Trim().Length
            If "0123456789P".IndexOf(inscr.Substring((intPos - 1), 1), 0, System.StringComparison.OrdinalIgnoreCase) + 1 > 0 Then
                strorigem = (strorigem + inscr.Substring((intPos - 1), 1))
            End If
        Next
        Select Case UF.ToUpper
            Case "AC"
                strbase = (strorigem.Trim() + "000000000").Substring(0, 9)
                If strbase.Substring(0, 2) = "01" AndAlso strbase.Substring(2, 2) <> "00" Then
                    intsoma = 0
                    For intPos = 1 To 8
                        intvalor = Integer.Parse(strbase.Substring((intPos - 1), 1))
                        intvalor = (intvalor * (10 - intPos))
                        intsoma = (intsoma + intvalor)
                    Next

                    intresto = intsoma Mod 11
                    strDigito1 = IIf(intresto < 2, Convert.ToString(11 - intresto).Substring(0, 0), Convert.ToString(11 - intresto).Length - 1)

                    strbase2 = (strbase.Substring(0, 8) + strDigito1)
                End If

                If strbase2 = strorigem Then

                    Return True
                End If

        End Select
    End Function



    'case "AL":

    'strBase = (strOrigem.Trim() + "000000000").Substring(0, 9);

    'if ((strBase.Substring(0, 2) == "24"))

    '{

    'intSoma = 0;

    'for (intPos = 1; (intPos <= 8); intPos++)

    '{

    'intValor = int.Parse(strBase.Substring((intPos - 1), 1));

    'intValor = (intValor * (10 - intPos));

    'intSoma = (intSoma + intValor);

    '}

    'intSoma = (intSoma * 10);

    'intResto = (intSoma % 11);

    'strDigito1 = ((intResto == 10) ? "0" : Convert.ToString(intResto)).Substring((((intResto == 10) ? "0" : Convert.ToString(intResto)).Length - 1));

    'strBase2 = (strBase.Substring(0, 8) + strDigito1);

    'if ((strBase2 == strOrigem))

    '{

    'retorno = true;

    '}

    '}

    'break;

    'case "AM":

    'strBase = (strOrigem.Trim() + "000000000").Substring(0, 9);

    'intSoma = 0;

    'for (intPos = 1; (intPos <= 8); intPos++)

    '{

    'intValor = int.Parse(strBase.Substring((intPos - 1), 1));

    'intValor = (intValor * (10 - intPos));

    'intSoma = (intSoma + intValor);

    '}

    'if ((intSoma < 11))

    '{

    'strDigito1 = Convert.ToString((11 - intSoma)).Substring((Convert.ToString((11 - intSoma)).Length - 1));

    '}

    'else

    '{

    'intResto = (intSoma % 11);

    'strDigito1 = ((intResto < 2) ? "0" : Convert.ToString((11 - intResto))).Substring((((intResto < 2) ? "0" : Convert.ToString((11 - intResto))).Length - 1));

    '}

    'strBase2 = (strBase.Substring(0, 8) + strDigito1);

    'if ((strBase2 == strOrigem))

    '{

    'retorno = true;

    '}

    'break;

    'case "AP":

    'strBase = (strOrigem.Trim() + "000000000").Substring(0, 9);

    'intPeso = 0;

    'intDig = 0;

    'if ((strBase.Substring(0, 2) == "03"))

    '{

    'intNumero = int.Parse(strBase.Substring(0, 8));

    'if (((intNumero >= 3000001)

    '&& (intNumero <= 3017000)))

    '{

    'intPeso = 5;

    'intDig = 0;

    '}

    'else if (((intNumero >= 3017001)

    '&& (intNumero <= 3019022)))

    '{

    'intPeso = 9;

    'intDig = 1;

    '}

    'else if ((intNumero >= 3019023))

    '{

    'intPeso = 0;

    'intDig = 0;

    '}

    'intSoma = intPeso;

    'for (intPos = 1; (intPos <= 8); intPos++)

    '{

    'intValor = int.Parse(strBase.Substring((intPos - 1), 1));

    'intValor = (intValor * (10 - intPos));

    'intSoma = (intSoma + intValor);

    '}

    'intResto = (intSoma % 11);

    'intValor = (11 - intResto);

    'if ((intValor == 10))

    '{

    'intValor = 0;

    '}

    'else if ((intValor == 11))

    '{

    'intValor = intDig;

    '}

    'strDigito1 = Convert.ToString(intValor).Substring((Convert.ToString(intValor).Length - 1));

    'strBase2 = (strBase.Substring(0, 8) + strDigito1);

    'if ((strBase2 == strOrigem))

    '{

    'retorno = true;

    '}

    '}

    'break;

    'case "BA":

    'strBase = (strOrigem.Trim() + "00000000").Substring(0, 8);

    'if ((("0123458".IndexOf(strBase.Substring(0, 1), 0, System.StringComparison.OrdinalIgnoreCase) + 1)

    '> 0))

    '{

    'intSoma = 0;

    'for (intPos = 1; (intPos <= 6); intPos++)

    '{

    'intValor = int.Parse(strBase.Substring((intPos - 1), 1));

    'intValor = (intValor * (8 - intPos));

    'intSoma = (intSoma + intValor);

    '}

    'intResto = (intSoma % 10);

    'strDigito2 = ((intResto == 0) ? "0" : Convert.ToString((10 - intResto))).Substring((((intResto == 0) ? "0" : Convert.ToString((10 - intResto))).Length - 1));

    'strBase2 = (strBase.Substring(0, 6) + strDigito2);

    'intSoma = 0;

    'for (intPos = 1; (intPos <= 7); intPos++)

    '{

    'intValor = int.Parse(strBase2.Substring((intPos - 1), 1));

    'intValor = (intValor * (9 - intPos));

    'intSoma = (intSoma + intValor);

    '}

    'intResto = (intSoma % 10);

    'strDigito1 = ((intResto == 0) ? "0" : Convert.ToString((10 - intResto))).Substring((((intResto == 0) ? "0" : Convert.ToString((10 - intResto))).Length - 1));

    '}

    'else

    '{

    'intSoma = 0;

    'for (intPos = 1; (intPos <= 6); intPos++)

    '{

    'intValor = int.Parse(strBase.Substring((intPos - 1), 1));

    'intValor = (intValor * (8 - intPos));

    'intSoma = (intSoma + intValor);

    '}

    'intResto = (intSoma % 11);

    'strDigito2 = ((intResto < 2) ? "0" : Convert.ToString((11 - intResto))).Substring((((intResto < 2) ? "0" : Convert.ToString((11 - intResto))).Length - 1));

    'strBase2 = (strBase.Substring(0, 6) + strDigito2);

    'intSoma = 0;

    'for (intPos = 1; (intPos <= 7); intPos++)

    '{

    'intValor = int.Parse(strBase2.Substring((intPos - 1), 1));

    'intValor = (intValor * (9 - intPos));

    'intSoma = (intSoma + intValor);

    '}

    'intResto = (intSoma % 11);

    'strDigito1 = ((intResto < 2) ? "0" : Convert.ToString((11 - intResto))).Substring((((intResto < 2) ? "0" : Convert.ToString((11 - intResto))).Length - 1));

    '}

    'strBase2 = (strBase.Substring(0, 6)

    '+ (strDigito1 + strDigito2));

    'if ((strBase2 == strOrigem))

    '{

    'retorno = true;

    '}

    'break;

    'case "CE":

    'strBase = (strOrigem.Trim() + "000000000").Substring(0, 9);

    'intSoma = 0;

    'for (intPos = 1; (intPos <= 8); intPos++)

    '{

    'intValor = int.Parse(strBase.Substring((intPos - 1), 1));

    'intValor = (intValor * (10 - intPos));

    'intSoma = (intSoma + intValor);

    '}

    'intResto = (intSoma % 11);

    'intValor = (11 - intResto);

    'if ((intValor > 9))

    '{

    'intValor = 0;

    '}

    'strDigito1 = Convert.ToString(intValor).Substring((Convert.ToString(intValor).Length - 1));

    'strBase2 = (strBase.Substring(0, 8) + strDigito1);

    'if ((strBase2 == strOrigem))

    '{

    'retorno = true;

    '}

    'break;

    'case "DF":

    'strBase = (strOrigem.Trim() + "0000000000000").Substring(0, 13);

    'if ((strBase.Substring(0, 3) == "073"))

    '{

    'intSoma = 0;

    'intPeso = 2;

    'for (intPos = 11; (intPos <= 1); intPos = (intPos + -1))

    '{

    'intValor = int.Parse(strBase.Substring((intPos - 1), 1));

    'intValor = (intValor * intPeso);

    'intSoma = (intSoma + intValor);

    'intPeso = (intPeso + 1);

    'if ((intPeso > 9))

    '{

    'intPeso = 2;

    '}

    '}

    'intResto = (intSoma % 11);

    'strDigito1 = ((intResto < 2) ? "0" : Convert.ToString((11 - intResto))).Substring((((intResto < 2) ? "0" : Convert.ToString((11 - intResto))).Length - 1));

    'strBase2 = (strBase.Substring(0, 11) + strDigito1);

    'intSoma = 0;

    'intPeso = 2;

    'for (intPos = 12; (intPos <= 1); intPos = (intPos + -1))

    '{

    'intValor = int.Parse(strBase.Substring((intPos - 1), 1));

    'intValor = (intValor * intPeso);

    'intSoma = (intSoma + intValor);

    'intPeso = (intPeso + 1);

    'if ((intPeso > 9))

    '{

    'intPeso = 2;

    '}

    '}

    'intResto = (intSoma % 11);

    'strDigito2 = ((intResto < 2) ? "0" : Convert.ToString((11 - intResto))).Substring((((intResto < 2) ? "0" : Convert.ToString((11 - intResto))).Length - 1));

    'strBase2 = (strBase.Substring(0, 12) + strDigito2);

    'if ((strBase2 == strOrigem))

    '{

    'retorno = true;

    '}

    '}

    'break;

    'case "ES":

    'strBase = (strOrigem.Trim() + "000000000").Substring(0, 9);

    'intSoma = 0;

    'for (intPos = 1; (intPos <= 8); intPos++)

    '{

    'intValor = int.Parse(strBase.Substring((intPos - 1), 1));

    'intValor = (intValor * (10 - intPos));

    'intSoma = (intSoma + intValor);

    '}

    'intResto = (intSoma % 11);

    'strDigito1 = ((intResto < 2) ? "0" : Convert.ToString((11 - intResto))).Substring((((intResto < 2) ? "0" : Convert.ToString((11 - intResto))).Length - 1));

    'strBase2 = (strBase.Substring(0, 8) + strDigito1);

    'if ((strBase2 == strOrigem))

    '{

    'retorno = true;

    '}

    'break;

    'case "GO":

    'strBase = (strOrigem.Trim() + "000000000").Substring(0, 9);

    'if ((("10,11,15".IndexOf(strBase.Substring(0, 2), 0, System.StringComparison.OrdinalIgnoreCase) + 1)

    '> 0))

    '{

    'intSoma = 0;

    'for (intPos = 1; (intPos <= 8); intPos++)

    '{

    'intValor = int.Parse(strBase.Substring((intPos - 1), 1));

    'intValor = (intValor * (10 - intPos));

    'intSoma = (intSoma + intValor);

    '}

    'intResto = (intSoma % 11);

    'if ((intResto == 0))

    '{

    'strDigito1 = "0";

    '}

    'else if ((intResto == 1))

    '{

    'intNumero = int.Parse(strBase.Substring(0, 8));

    'strDigito1 = (((intNumero >= 10103105)

    '&& (intNumero <= 10119997)) ? "1" : "0").Substring(((((intNumero >= 10103105)

    '&& (intNumero <= 10119997)) ? "1" : "0").Length - 1));

    '}

    'else

    '{

    'strDigito1 = Convert.ToString((11 - intResto)).Substring((Convert.ToString((11 - intResto)).Length - 1));

    '}

    'strBase2 = (strBase.Substring(0, 8) + strDigito1);

    'if ((strBase2 == strOrigem))

    '{

    'retorno = true;

    '}

    '}

    'break;

    'case "MA":

    'strBase = (strOrigem.Trim() + "000000000").Substring(0, 9);

    'if ((strBase.Substring(0, 2) == "12"))

    '{

    'intSoma = 0;

    'for (intPos = 1; (intPos <= 8); intPos++)

    '{

    'intValor = int.Parse(strBase.Substring((intPos - 1), 1));

    'intValor = (intValor * (10 - intPos));

    'intSoma = (intSoma + intValor);

    '}

    'intResto = (intSoma % 11);

    'strDigito1 = ((intResto < 2) ? "0" : Convert.ToString((11 - intResto))).Substring((((intResto < 2) ? "0" : Convert.ToString((11 - intResto))).Length - 1));

    'strBase2 = (strBase.Substring(0, 8) + strDigito1);

    'if ((strBase2 == strOrigem))

    '{

    'retorno = true;

    '}

    '}

    'break;

    'case "MT":

    'strBase = (strOrigem.Trim() + "0000000000").Substring(0, 10);

    'intSoma = 0;

    'intPeso = 2;

    'for (intPos = 10; (intPos <= 1); intPos = (intPos + -1))

    '{

    'intValor = int.Parse(strBase.Substring((intPos - 1), 1));

    'intValor = (intValor * intPeso);

    'intSoma = (intSoma + intValor);

    'intPeso = (intPeso + 1);

    'if ((intPeso > 9))

    '{

    'intPeso = 2;

    '}

    '}

    'intResto = (intSoma % 11);

    'strDigito1 = ((intResto < 2) ? "0" : Convert.ToString((11 - intResto))).Substring((((intResto < 2) ? "0" : Convert.ToString((11 - intResto))).Length - 1));

    'strBase2 = (strBase.Substring(0, 10) + strDigito1);

    'if ((strBase2 == strOrigem))

    '{

    'retorno = true;

    '}

    'break;
End Class
